package com.example.work4.view

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import androidx.core.content.ContextCompat.startActivity
import com.example.work4.ChatActivity
import com.example.work4.DataUtil
import com.example.work4.MomentsActivity
import com.example.work4.R
import com.example.work4.common.ImageUtil
import com.example.work4.http.FriendRetrofitImp
import com.example.work4.http.body.po.PersonalInfoPage
import com.example.work4.po.ResultBean
import retrofit2.Call
import retrofit2.Response

@SuppressLint("StaticFieldLeak")
object PersonalInfoDialog :View.OnClickListener {

    var follow: Button?=null
    var nickname: TextView? = null
    var email: TextView? = null
    var avatar: ImageView? = null
    var img1: ImageView? = null
    var img2: ImageView?= null
    var img3: ImageView? = null
    val dialog: Dialog? = null
    var context: Context?=null
    var layout:LinearLayout? = null
    var go2chat:Button?=null
    var id : Int = 0;
    var avatarLink:String = "";
    fun show(context: Context, info: PersonalInfoPage){
        this.context = context;
        val myView: View =
            LayoutInflater.from(context).inflate(R.layout.dialog_personal_info, null)
        myView.setPadding(0, 0, 0, 0)
        val dialog: Dialog = AlertDialog.Builder(context)
            .setView(myView).create()
        dialog.window?.setGravity(Gravity.CENTER)
        follow = myView.findViewById(R.id.follow)
        nickname = myView.findViewById(R.id.nickname)
        email = myView.findViewById(R.id.email)
        avatarLink = info.avatar!!
        go2chat = myView.findViewById(R.id.go2chat)
        go2chat!!.setOnClickListener(this)
        avatar = myView.findViewById(R.id.avatar)
        img1 = myView.findViewById(R.id.img1)
        img2 = myView.findViewById(R.id.img2)
        img3 = myView.findViewById(R.id.img3)
        layout = myView.findViewById(R.id.imageLayout)
        id = info.userId
        layout!!.setOnClickListener(this)
        follow?.setOnClickListener(this)
        init(info)
        dialog.show()
    }

    private fun init(info: PersonalInfoPage) {
        follow?.text =info.friendType
            nickname?.text = info.username
        email?.text = info.email
        val imageUtil= context?.let { ImageUtil(it) }
        avatar?.let { imageUtil?.GlideLoadCenterCrop(info.avatar, it) }
        if(info.momentImgList!=null){
            if(info.momentImgList.size>0)imageUtil?.GlideLoadCenterCrop(info.momentImgList[0], img1!!)
            if(info.momentImgList.size>1)imageUtil?.GlideLoadCenterCrop(info.momentImgList[1], img2!!)
            if(info.momentImgList.size>2)imageUtil?.GlideLoadCenterCrop(info.momentImgList[2], img3!!)
        }
    }



    override fun onClick(v: View?) {
        when (v!!.id){
            R.id.imageLayout ->{
                val intent = Intent()
                intent.putExtra("id", id)
                intent.putExtra("avatar",avatarLink)
                intent.putExtra("nickname",nickname!!.text.toString())
                intent.setClass(context!!,MomentsActivity::class.java)
                context!!.startActivity(intent)
            }
            R.id.go2chat ->{
                val intent = Intent()
                intent.putExtra("id", id)
                intent.putExtra("avatar",avatarLink)
                intent.putExtra("nickname",nickname!!.text.toString())
                intent.setClass(context!!,ChatActivity::class.java)
                context!!.startActivity(intent)
            }

            R.id.follow ->{
                val friendRetrofitImp = FriendRetrofitImp()
                if(follow?.text!! == "已互关" || follow?.text!! == "已关注"){
                    friendRetrofitImp.delCall(id).enqueue(object : retrofit2.Callback<ResultBean<Map<String, String>>> {
                        override fun onResponse(call: Call<ResultBean<Map<String, String>>>,
                                                response: Response<ResultBean<Map<String, String>>>) {
                            if (response.code() == 200) {
                                Toast.makeText(context, "取关成功！", Toast.LENGTH_SHORT).show()
                                val text = response.body()!!.data!!["friendType"]
                                follow!!.text = text
                                DataUtil.messageHandler!!.sendEmptyMessage(DataUtil.UPDATE_FRIEND_LIST)
                            }
                        }
                        override fun onFailure(call: Call<ResultBean<Map<String, String>>>,
                                               t: Throwable) {

                        }
                    })

                }
                else{
                    friendRetrofitImp.addCall(id).enqueue(object : retrofit2.Callback<ResultBean<Map<String, String>>> {
                        override fun onResponse(call: Call<ResultBean<Map<String, String>>>,
                                                response: Response<ResultBean<Map<String, String>>>) {
                            if (response.code() == 200) {
                                Toast.makeText(context, "关注成功！", Toast.LENGTH_SHORT).show()
                                val text = response.body()!!.data!!["friendType"]
                                follow!!.text = text
                                DataUtil.messageHandler!!.sendEmptyMessage(DataUtil.UPDATE_FRIEND_LIST)
                            }
                        }
                        override fun onFailure(call: Call<ResultBean<Map<String, String>>>,
                                               t: Throwable) {

                        }
                    })


                }
            }
        }

    }



    fun close(){
        dialog?.dismiss()
    }
}